package com.wwj.core.system.service.impl;

import com.wwj.core.system.entity.Role;
import com.wwj.core.system.entity.RoleMenu;
import com.wwj.core.system.entity.mapper.RoleMapper;
import com.wwj.core.system.entity.mapper.RoleMenuMapper;
import com.wwj.core.system.entity.mapper.UserRoleMenuMapper;
import com.wwj.core.system.service.IRoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by xya on 16-10-24.
 */
@Service
public class RoleServiceImpl implements IRoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private UserRoleMenuMapper userRoleMenuMapper;

    @Override
    public Role findRoleById(Integer id) throws Exception {
        Role role = roleMapper.getById(id);
        if (null != role)
            role.setMenuIds(roleMenuMapper.selectMenuIdsByRoleId(role.getId()));
        return role;
    }

    @Override
    public Page findRoleByPage(int pageNo, int pageSize, String search) {
        Map<String, String> params = null;
        if (null != search && !"".equals(search)) {
            params = new HashMap<String, String>(1);
            params.put("search", search);
        }
        PageHelper.startPage(pageNo, pageSize);
        List<Role> list = roleMapper.selectWithParams(params);
        return (Page) list;
    }

    @Override
    public List<Role> findAllRole() throws Exception {
        List<Role> roles = roleMapper.select();
        for (Role role : roles) {
            Integer[] menuIds = roleMenuMapper.selectMenuIdsByRoleId(role.getId());
            role.setMenuIds(menuIds);
        }
        return roles;
    }

    @Override
    public Set<String> getRoleAsString(String userId) {
        List<Role> roleList = userRoleMenuMapper.selectRoleByUser(userId);
        if (CollectionUtils.isEmpty(roleList)) {
            return null;
        }
        Set<String> set = new HashSet<String>();
        for (Role r : roleList) {
            set.add(r.getCode());
        }
        return set;
    }

    @Override
    public int createRole(Role role) throws Exception {
        return roleMapper.save(role);
    }


    @Override
    public int removeRole(Integer[] ids) throws Exception {
        for (Integer id : ids){
            roleMenuMapper.deleteByRoleId(id);
            roleMapper.delete(id);
        }
        return 1;
    }

    @Override
    public int modifyRole(Role role) throws Exception {
        return roleMapper.update(role);
    }

    @Override
    public int createRoleMenu(Integer id, Integer[] menuIds) throws Exception {
        int num = 0;
        for (Integer menuId : menuIds)
            num += roleMenuMapper.save(new RoleMenu(id, menuId));
        return num;
    }

    @Override
    public boolean validateId(Integer id) throws Exception {
        return roleMapper.validateId(id) > 0;
    }

    @Override
    public boolean validateCode(Integer id, String code) throws Exception {
        if (null != id) {
            Role role = roleMapper.getById(id);
            if (code.equals(role.getCode())) {
                return false;
            }
        }
        return roleMapper.validateCode(code) > 0;
    }

    @Override
    public int saveOrUpdate(Role role) throws Exception {
        int num;
        if (null != role.getId()) {
            Role persist = roleMapper.getById(role.getId());
            persist.setName(role.getName());
            num = roleMapper.update(role);
            roleMenuMapper.deleteByRoleId(role.getId());
        } else {
            num = roleMapper.save(role);
        }
        for (Integer menuId : role.getMenuIds()) {
            RoleMenu rm = new RoleMenu(role.getId(), menuId);
            roleMenuMapper.save(rm);
        }
        return num;
    }

    @Override
    public boolean checkRoleCode(String value) throws Exception {
        return roleMapper.checkRoleCode(value) > 0;
    }

    @Override
    public boolean checkRoleName(String value) throws Exception {
        return roleMapper.checkRoleName(value) > 0;
    }


}
